<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



if(!function_exists('JFX_PagesMenu_MakeTree')){


function JFX_PagesMenu_MakeTree($arr){
    $CONFIG = JFX::registry('config');
    $USER = JFX::registry('JFX_User');
    $LANG = JFX::registry('lang');
    $returnString = '';
    foreach($arr as $k=>$v){

        if($USER->checkAdminPermission('core', 'perm_page_'.$USER->workspace.'_'.$v['id'])){
            $link = '<a href="'.$CONFIG->adminUrl.'/edit-page/?page='.$v['id'].'">'.$LANG->getContent('core', 'page_title', $v['id']).'</a>';
            $canEditLink = true;
        }else{
            $link = $LANG->getContent('core', 'page_title', $v['id']);
            $canEditLink = false;
        }

        if((int) $v['parent_id'] > 0 && $USER->checkAdminPermission('core', 'perm_delete_pages') && $canEditLink){
                $link .= '&nbsp;&nbsp;&nbsp;<a class="jfx-delete" href="'.$CONFIG->adminUrl.'/delete-page/?page='.$v['id'].'">
                    <img src="'.$CONFIG->imageUrl.'/admin/icons/delete.png" alt="Delete" title="Delete Page" /></a>';
        }

        if(isset($v['subpages']) && count($v['subpages'])>0){
            $subpages = '<ul>'.JFX_PagesMenu_MakeTree($v['subpages']).'</ul>';
        }else{
            $subpages = '';
        }
        $returnString .= '<li>'.$link.$subpages.'</li>';
    }

    return $returnString;
}


}



$js = <<<JS
$(document).ready(function(){
    $('#jfx-pages-menu-close').hover(function(){
        $('img', this).attr('src', $('img', this).attr('src').replace('-normal', '-hover'));
    },function(){
        $('img', this).attr('src', $('img', this).attr('src').replace('-hover', '-normal'));
    });
    $('#jfx-pages-menu-close').click(function(){
        $('#jfx-pages-menu').fadeOut('slow');
    });
});
JS;

$VIEW->addJs($js);



$pageid = (int) get('page');

$pagesRecursive = JFX::getPageTree(0, $USER->workspace);

$tree = '<ul id="jfxcore-draggable-page-tree">'.JFX_PagesMenu_MakeTree($pagesRecursive).'</ul>';

$SMARTY->assign('tree', $tree);
$SMARTY->assign('pageid', $pageid);

$VIEW->addTemplate('main', 'admin/ajax/pages-menu.tpl');


